﻿<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8" />
    <title>选择岗位</title>
    <link href="../../../Comm/Style/CommStyle.css" rel="stylesheet" type="text/css" />
    <link href="../../../../DataUser/Style/ccbpm.css" rel="stylesheet" type="text/css" />
    <link href="../../../Scripts/easyUI/themes/default/easyui.css" rel="stylesheet" type="text/css" />
    <link href="../../../Scripts/easyUI/themes/icon.css" rel="stylesheet" type="text/css" />
    <script src="../../../Scripts/jquery-1.7.2.min.js" type="text/javascript"></script>
    <script src="../../../Scripts/easyUI/jquery.easyui.min.js" type="text/javascript"></script>
    <script src="../../../Scripts/QueryString.js" type="text/javascript"></script>
    <script type="text/javascript" src="../../../Scripts/config.js"></script>
    <script type="text/javascript" src="../../../Comm/Gener.js"></script>
    <script src="../../../Scripts/EasyUIUtility.js" type="text/javascript"></script>

    <base target="_self" />
    <script type="text/javascript">

        //页面启动函数.
        $(function () {
            $("#Msg").html("<img src=../../../Img/loading.gif />&nbsp;正在加载,请稍后......");

            //求出来已经设置的延续子流程.
            var nodeID = GetQueryString("FK_Node");

            //已经选择的岗位.
            var ens = new Entities("BP.WF.Template.NodeStations");
            ens.Retrieve("FK_Node", nodeID);

            //获得该组织的岗位.
            var stas = new Entities("BP.Port.Stations");
            stas.RetrieveAll();

            //if (webUser.OrgNo == null)
            //  stas.Retrieve("OrgNo", orgNo);
            //else


            if (stas.length == 0) {
                $("#Msg").html("<span style='color:red;margin:10px'>组织编号为" + orgNo + "下面没有设置岗位</span>");
                return;
            }

            var stationTypes = new Entities("BP.Port.StationTypes");
            stationTypes.RetrieveAll();

            BindGroupModel(stationTypes, stas, ens);

        });

        function BindGroupModel(groups, entities, selectedEles) {

            AttrOfMInMM = "FK_Station";
            var html = "<table style='width:100%;' >";

            html += "<tr>";
            html += "<th colspan=3>";
            html += "<div style='float:left'>";
            html += "<input type=button value='保存' onclick='Save();' id='save' />";
            html += "</div>";

            html += "</th>";
            html += "</tr>";

            for (var i = 0; i < groups.length; i++) {

                var group = groups[i];

                html += "<tr>";
                html += "<th colspan=3>";

                html += "<input type='checkbox'  onclick='CheckAll(this.checked, \"" + group.No + "\");' id='Group_" + group.No + "'  /><label for='Group_" + group.No + "'>" + group.Name + "</label>";

                html += "</th>";
                html += "</tr>";


                var myNum = -1;
                for (var idx = 0; idx < entities.length; idx++) {

                    en = entities[idx];

                    var myidx = 0;
                    var oOfEn;
                    for (var obj in en) {
                        if (myidx == 2) {
                            oOfEn = obj;
                            break;
                        }
                        myidx++;
                    }

                    myidx = 0;
                    var oOfGroup;
                    for (var obj in en) {
                        if (myidx == 0) {
                            oOfGroup = obj;
                            break;
                        }
                        myidx++;
                    }

                    if (en[oOfEn] != group[oOfGroup])
                        continue;

                    var isHave = false;
                    for (var iEle = 0; iEle < selectedEles.length; iEle++) {

                        var ele = selectedEles[iEle];
                        if (ele == null || ele == undefined)
                            continue;

                        if (ele[AttrOfMInMM] == en.No) {
                            isHave = true;
                            break;
                        }
                    }

                    var lab = en.Name;
                    if (isHave)
                        lab = "<font color=green>" + en.Name + "</font>";

                    myNum++;
                    if (myNum == 0)
                        html += "<TR>";

                    if (isHave == true)
                        html += "<td><input type='checkbox' checked=true name='" + group.No + "' id='CB_" + en.No + "' value='" + en.No + "' /><label for='CB_" + en.No + "'>" + lab + "</label></td>";
                    else
                        html += "<td><input type='checkbox'  name='" + group.No + "' id='CB_" + en.No + "' value='" + en.No + "' /><label for='CB_" + en.No + "'>" + lab + "</label></td>";


                    if (myNum == 2) {
                        html += "</tr>";
                        myNum = -1;
                    }
                }

                if (idx == 1) {
                    html += "<td colspan=2></td>";
                    html += "</tr>";
                }

                if (idx == 2) {
                    html += "<td></td>";
                    html += "</tr>";
                }
            }

            html += "</table>";

            console.log(html);
            $("#Msg").html("");
            $("#Msg").html(html);
        }

        //选择与取消选择.
        function CheckAll(checked, no) {

            $("input[name='" + no + "']").each(function () {
                this.checked = checked;
            });

        }

        //保存.
        function Save() {

            var nodeID = GetQueryString("FK_Node");
            var orgNo = GetQueryString("OrgNo");

            //删除,该组织下已经保存的岗位.
            var sql = "DELETE FROM WF_NodeStation WHERE FK_Station IN (SELECT No FROM Port_Station WHERE OrgNo='" + orgNo + "') AND FK_Node=" + nodeID;
            DBAccess.RunSQL(sql);
            //获得该组织的岗位.
            var stas = new Entities("BP.Port.Stations");
            stas.Retrieve("OrgNo", orgNo);

            //遍历checkbox控件，然后执行插入.
            for (var i = 0; i < stas.length; i++) {
                var en = new Entity("BP.WF.Template.NodeStation");

                if (document.getElementById("CB_" + stas[i].No).checked) {
                    en.FK_Node = nodeID;
                    en.FK_Station = stas[i].No;
                    en.Insert();
                }
            }

            window.close();
        }

    </script>
</head>
<body>


    <div id="Msg"></div>
</body>
</html>
